/*
单例模式
饿汉模式
class Singleton {
    // 私有静态成员变量，在类加载时就初始化实例
    private static Singleton instance = new Singleton();
    // 私有构造函数，防止外部通过new关键字创建实例
    private Singleton() {}
    // 公共静态方法，用于获取单例实例
    public static Singleton getInstance() {
        return instance;
    }
}*/

/*
懒汉模式（单线程版）线程不安全
class Singleton {
    private static Singleton instance ;
    private Singleton() {}
    public static Singleton getInstance() {
        if(instance == null) {
            instance = new Singleton();
        }
        return  instance;
    }
}*/
/* 加锁版  线程安全
class Singleton {
    private static Singleton instance ;
    private  Singleton() {}
    public static synchronized Singleton getInstance() {
        if(instance == null) {
            instance = new Singleton();
        }
        return  instance;
    }
}*/
class Singleton {
    private static volatile Singleton instance ;
    private  Singleton() {}

    public static Singleton getInstance() {
        if(instance == null) {
            synchronized (Singleton.class) {
                if(instance == null) {
                    instance = new Singleton();
                }
            }
        }
        return instance;
    }
}

